/*
 * Copyright 2008 Andrej Herich
 * 
 * http://azetclient.yw.sk/
 * http://azetclient.googlecode.com/
 * 
 * This file is part of AzetClient.
 * 
 * AzetClient is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AzetClient is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AzetClient.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.yw.azetclient.gui;

import java.awt.Component;
import java.awt.Font;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.ListCellRenderer;
import javax.swing.table.TableCellRenderer;
import org.apache.log4j.Logger;
import sk.yw.azetclient.Main;
import sk.yw.azetclient.model.Buddy;

/**
 *
 * @author error216
 */
public class BuddyCellRenderer implements ListCellRenderer, TableCellRenderer {
    
    private static final Logger logger =
            Logger.getLogger(BuddyCellRenderer.class);
    
    public BuddyCellRenderer() {
    }
    
    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        return getCellRendererComponent(list, value, isSelected, cellHasFocus);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        return getCellRendererComponent(table, value, isSelected, hasFocus);
    }
    
    public Component getCellRendererComponent(Component base, Object value,
            boolean isSelected, boolean cellHasFocus) {
        JLabel component = new JLabel();

        if (value == null) {
            component.setText("null");
        } else if (!(value instanceof Buddy)) {
            component.setText(value.toString());
        } else {
            Buddy buddy = (Buddy) value;
            if (buddy.equals(BuddyListModel.getSeparator())) {
                return new JSeparator();
            } else {
                component.setText(buddy.getVisualName());
                if (buddy.getStatus() == null) {
                    component.setFont(getOfflineFont(base));
                    logger.warn("Null pointer in buddy status.");
                } else {
                    switch (buddy.getStatus()) {
                        case CHATTING:
                            component.setFont(getChattingFont(base));
                            break;
                        case ONLINE:
                            component.setFont(getOnlineFont(base));
                            break;
                        case OFFLINE:
                            component.setFont(getOfflineFont(base));
                            break;
                    }
                }
                
                StringBuilder toolTip = new StringBuilder();
                toolTip.append("<html>");
                toolTip.append("<b><font size=\"+1\">");
                toolTip.append(buddy.getName());
                toolTip.append("</font></b><br/>");
                if (buddy.getStatus() == null) {
                    logger.warn("Null pointer in buddy status.");
                } else {
                    switch (buddy.getStatus()) {
                        case CHATTING:
                            toolTip.append("<b>");
                            toolTip.append(Main.getText(
                                    "buddyList.toolTip.chatting"));
                            if (buddy.getRoom() != null
                                    && !buddy.getRoom().isEmpty()) {
                                toolTip.append(", ");
                                toolTip.append(buddy.getRoom());
                            }
                            toolTip.append("</b>");
                            break;
                        case ONLINE:
                            toolTip.append("<b>");
                            toolTip.append(Main.getText(
                                    "buddyList.toolTip.online"));
                            toolTip.append("</b>");
                            break;
                        case OFFLINE:
                            toolTip.append("<b>");
                            toolTip.append(Main.getText(
                                    "buddyList.toolTip.lastLogin"));
                            toolTip.append(": ");
                            toolTip.append("</b>");
                            DateFormat dateFormat = new SimpleDateFormat(
                                    Main.getText(
                                    "buddyList.toolTip.dateFormat"));
                            toolTip.append(
                                    dateFormat.format(buddy.getLastLogin()));
                            break;
                    }
                }
                toolTip.append("<br/>");
                toolTip.append("<b>");
                toolTip.append(Main.getText("buddyList.toolTip.gender"));
                toolTip.append(": ");
                toolTip.append("</b>");
                if (buddy.getGender() == null) {
                    toolTip.append("-");
                    logger.warn("Null pointer in buddy gender.");
                } else {
                    switch (buddy.getGender()) {
                        case MALE:
                            toolTip.append(Main.getText(
                                    "buddyList.toolTip.gender.male"));
                            break;
                        case FEMALE:
                            toolTip.append(Main.getText(
                                    "buddyList.toolTip.gender.female"));
                            break;
                    }
                }
                toolTip.append("<br/>");
                toolTip.append("<b>");
                toolTip.append(Main.getText("buddyList.toolTip.age"));
                toolTip.append(": ");
                toolTip.append("</b>");
                if (buddy.getAge() > -1) {
                    toolTip.append(buddy.getAge());
                } else {
                    toolTip.append("-");
                }
                toolTip.append("<br/>");
                toolTip.append("<b>");
                toolTip.append(Main.getText("buddyList.toolTip.location"));
                toolTip.append(": ");
                toolTip.append("</b>");
                toolTip.append(buddy.getLocation());
                toolTip.append("<br/>");
                toolTip.append("<html>");
                component.setToolTipText(toolTip.toString());
            }
        }
        
        component.setOpaque(true);
        if (base instanceof JList) {
            JList list = (JList) base;
            if (isSelected) {
                component.setBackground(list.getSelectionBackground());
                component.setForeground(list.getSelectionForeground());
            } else {
                component.setBackground(list.getBackground());
                component.setForeground(list.getForeground());
            }
        }
        if (base instanceof JTable) {
            JTable table = (JTable) base;
            if (isSelected) {
                component.setBackground(table.getSelectionBackground());
                component.setForeground(table.getSelectionForeground());
            } else {
                component.setBackground(table.getBackground());
                component.setForeground(table.getForeground());
            }
        }

        return component;
    }
    
    private Font getChattingFont(Component base) {
        return base.getFont().deriveFont(Font.BOLD);
    }
    
    private Font getOnlineFont(Component base) {
        return base.getFont().deriveFont(Font.BOLD | Font.ITALIC);
    }
    
    private Font getOfflineFont(Component base) {
        return base.getFont().deriveFont(Font.PLAIN);
    }
    
}
